using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class Denormal
    {
        internal Database db;
        private int itemId;
        private int typeId;
        private int groupId;
        private int solarSystemId;
        private int constellationId;
        private int regionId;
        private int orbitId;
        private double x, y, z;
        private double radius;
        private string itemName;
        private double security;
        private int celestialIndex;
        private int orbitIndex;

        private EveObject item;
        private Type type;
        private Group group;
        private SolarSystem solarSystem;
        private Constellation constellation;
        private Region region;
        private Vector location;
        private Celestial orbit;

        public EveObject Item
        {
            get
            {
                if (item == null)
                    item = EveData.GetObject(db, itemId);
                return item;
            }
        }
        public Type Type
        {
            get
            {
                if (type == null)
                    type = new Type(db, typeId);
                return type;
            }
        }
        public Group Group
        {
            get
            {
                if (group == null)
                    group = new Group(db, groupId);
                return group;
            }
        }
        public SolarSystem SolarSystem
        {
            get
            {
                if (solarSystem == null)
                    solarSystem = new SolarSystem(db, solarSystemId);
                return solarSystem;
            }
        }
        public Constellation Constellation
        {
            get
            {
                if (constellation == null)
                    constellation = new Constellation(db, constellationId);
                return constellation;
            }
        }
        public Region Region
        {
            get
            {
                if (region == null)
                    region = new Region(db, regionId);
                return region;
            }
        }
        public Celestial Orbit
        {
            get
            {
                if (orbit == null)
                    orbit = new Celestial(db, orbitId);
                return orbit;
            }
        }
        public Vector Location
        {
            get
            {
                if (location == null)
                    location = new Vector(x, y, z);
                return location;
            }
        }
        public double Radius
        {
            get { return radius; }
        }
        public string Name
        {
            get { return itemName; }
        }
        public double Security
        {
            get { return security; }
        }
        public int CelestialIndex
        {
            get { return celestialIndex; }
        }
        public int OrbitIndex
        {
            get { return orbitIndex; }
        }

        internal Denormal(DbDataRow dr)
        {
            this.db = dr.Database;
            Fill(dr);
        }
        internal Denormal(Database db, int id)
        {
            this.db = db;
            Fill(db.ExecuteSingle("SELECT * FROM mapDenormalize WHERE itemID=" + id));
        }

        private void Fill(DbDataRow dr)
        {
            itemId = dr["itemID"];
            typeId = dr["typeID"];
            groupId = dr["groupID"];
            solarSystemId = dr["solarSystemID"];
            constellationId = dr["constellationID"];
            regionId = dr["regionID"];
            orbitId = dr["orbitID"];
            x = dr["x"];
            y = dr["y"];
            z = dr["z"];
            radius = dr["radius"];
            itemName = dr["itemName"];
            security = dr["security"];
            celestialIndex = dr["celestialIndex"];
            orbitIndex = dr["orbitIndex"];
        }
    }
}
